﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormStore.实体库;
using WinFormStore.数据层;
using WinFormStore.通信层;
using static WinFormStore.FrmMain;

namespace WinFormStore.Erp
{
    public partial class FrmGoodPutIn : Form
    {
        public ScannerComService comService = null;//声明数据层对象
         //配置文件路径
        public string path;
        //配置服务对象
        public ScannerInfoService infoService;
        //配置实体对象
        public ScannerInfo scannerInfo;

        public FrmGoodPutIn()
        {
            InitializeComponent();
        }

        public FrmGoodPutIn(string path, ScannerInfoService infoService, ScannerInfo scannerInfo)
        {
            InitializeComponent();
            this.path = path;
            this.infoService = infoService;
            this.scannerInfo = scannerInfo;

            //this.FormClosing += new FormClosingEventHandler(FrmGoodPutIn_FormClosing);
        }
        private List<string> codeList = new List<string>();//所有条码集合

        private void BindType()
        {
            DataTable dt = new DataTable();
            DBHelper.PrepareSql("select * from [Type]");
            dt = DBHelper.ExecuteQuery();
            DataRow dr = dt.NewRow();
            dr["TypeName"] = "--请选择--";
            dr["TypeId"] = "0";
            dt.Rows.InsertAt(dr, 0);
            this.cmbTypeID.DataSource = dt;
            this.cmbTypeID.DisplayMember = "TypeName";
            this.cmbTypeID.ValueMember = "TypeID";
        }
        private void ComService_CodeReceived(string code)
        {
            //空码判断
            if (code.Length == 0)
            {
                AddLog("条形码为空");
                return;
            }
            //已存在判断
            if (codeList.Contains(code))
            {
                this.Invoke(new EventHandler(delegate
                {
                   
                }));

            }
            //如果有需要，可以添加其他特殊判断...
            //正确读取显示
            codeList.Add(code);
            AddLog(code);
            ReadInfo();
        }
        private void AddLog(string message)
        {
            if (this.txtBarCode.InvokeRequired)
            {
                this.txtBarCode.Invoke(new Action<string>(AddLog), message);

            }
            else
            {
                this.txtBarCode.Text = message;
            }
        }
        private void FrmGoodPutIn_Load(object sender, EventArgs e)
        {
            comService = new ScannerComService();//实例化数据层对象
            comService.Connect(scannerInfo);
            comService.CodeReceived += ComService_CodeReceived;
            BindType();
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            scannerInfo = null;
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            DBHelper.PrepareSql("select * from Goods where BarCode=@BarCode");
            DBHelper.SetParameter("BarCode", this.txtBarCode.Text);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            if (dt.Rows.Count == 0)
            {
                //新增
                string sql = "insert into Goods(BarCode,TypeID,GoodsName,StorePrice,SalePrice,Discount,StockNum,StockDate) values(@BarCode,@TypeID,@GoodsName,@StorePrice,@SalePrice,@Discount,@StockNum,getdate())";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("BarCode", this.txtBarCode.Text);
                DBHelper.SetParameter("GoodsName", this.txtGoodsName.Text);
                DBHelper.SetParameter("TypeID", this.cmbTypeID.SelectedValue.ToString());
                DBHelper.SetParameter("StorePrice", this.txtStorePrice.Text);
                DBHelper.SetParameter("SalePrice", this.txtSalePrice.Text);
                DBHelper.SetParameter("Discount", this.txtDiscount.Text);
                DBHelper.SetParameter("StockNum", this.txtStockNum.Text);
                int rowCount = DBHelper.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    MessageBox.Show("入库成功!");
                    
                }
                else
                {
                    MessageBox.Show("入库失败!");
                }
            }
            else
            {
                //修改
                string sql = "update Goods set TypeID=@TypeID,GoodsName=@GoodsName,StorePrice=@StorePrice,SalePrice=@SalePrice,Discount=@Discount,StockNum=StockNum+@StockNum,StockDate=getdate() where BarCode=@BarCode";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("TypeID", this.cmbTypeID.SelectedValue.ToString());
                DBHelper.SetParameter("GoodsName", this.txtGoodsName.Text);
                DBHelper.SetParameter("StorePrice", this.txtStorePrice.Text);
                DBHelper.SetParameter("SalePrice", this.txtSalePrice.Text);
                DBHelper.SetParameter("Discount", this.txtDiscount.Text);
                DBHelper.SetParameter("StockNum", this.txtStockNum.Text);
                DBHelper.SetParameter("BarCode", this.txtBarCode.Text);
                int rowCount = DBHelper.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    MessageBox.Show("入库成功!");

                }
                else
                {
                    MessageBox.Show("入库失败!");
                }
            }
        }
        private void ReadInfo()
        {
            DBHelper.PrepareSql("select * from Goods where BarCode=@BarCode");
            DBHelper.SetParameter("BarCode", this.txtBarCode.Text);
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            if (dt.Rows.Count == 0)
            {
                this.Invoke(new EventHandler(delegate
                {
                    MessageBox.Show("该商品为录入!");
                    this.txtGoodsName.Text = "";
                    this.cmbTypeID.SelectedValue = 0;
                    this.txtStorePrice.Text = "";
                    this.txtSalePrice.Text = "";
                    this.txtDiscount.Text = "";
                    this.txtStockNum.Text = "";
                }));
            }
            else
            {
                this.Invoke(new EventHandler(delegate
                {
                    this.txtGoodsName.Text = dt.Rows[0]["GoodsName"].ToString();
                    this.cmbTypeID.SelectedValue = dt.Rows[0]["TypeID"].ToString();
                    this.txtStorePrice.Text = dt.Rows[0]["StorePrice"].ToString();
                    this.txtSalePrice.Text = dt.Rows[0]["SalePrice"].ToString();
                    this.txtDiscount.Text = dt.Rows[0]["Discount"].ToString();
                    this.txtStockNum.Text = "0";//dt.Rows[0]["StockNum"].ToString()
                }));
              
            }
        }
        private void btnReadInfo_Click(object sender, EventArgs e)
        {
            //DBHelper.PrepareSql("select * from Goods where BarCode=@BarCode");
            //DBHelper.SetParameter("BarCode",this.txtBarCode.Text);
            //DataTable dt = new DataTable();
            //dt=DBHelper.ExecuteQuery();
            //if (dt.Rows.Count == 0)
            //{
            //    MessageBox.Show("该商品为录入!");
            //}
            //else
            //{
            //    this.txtGoodsName.Text = dt.Rows[0]["GoodsName"].ToString();
            //    this.cmbTypeID.SelectedValue = dt.Rows[0]["TypeID"].ToString();
            //    this.txtStorePrice.Text = dt.Rows[0]["StorePrice"].ToString();
            //    this.txtSalePrice.Text = dt.Rows[0]["SalePrice"].ToString();
            //    this.txtDiscount.Text = dt.Rows[0]["Discount"].ToString();
            //    this.txtStockNum.Text = dt.Rows[0]["StockNum"].ToString();
            //}
            ReadInfo();
        }

        private void FrmGoodPutIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.comService?.DisConn();
        }
    }
}
